Firebase ব্যবহার করে Push Notification এর বাস্তব উদাহরণ

Mobile App Development - কোর্ডভা (Cordova) - Cordova এবং Firebase Integration
240

Firebase একটি Google এর পক্ষ থেকে প্রদত্ত একটি মোবাইল এবং ওয়েব অ্যাপ ডেভেলপমেন্ট প্ল্যাটফর্ম, যা বিভিন্ন ফিচার যেমন ডাটাবেস, অথেনটিকেশন, হোস্টিং, এবং Push Notifications প্রদান করে। Firebase Cloud Messaging (FCM) একটি ফ্রি সেবা, যা আপনাকে মোবাইল বা ওয়েব অ্যাপ্লিকেশনে রিয়েল-টাইম নোটিফিকেশন পাঠানোর সুযোগ দেয়।

এখানে Firebase Cloud Messaging (FCM) ব্যবহার করে Push Notification পাঠানোর একটি বাস্তব উদাহরণ দেয়া হলো।

১. Firebase প্রজেক্ট সেটআপ করা

প্রথমে আপনাকে Firebase Console এ গিয়ে একটি প্রজেক্ট তৈরি করতে হবে এবং আপনার অ্যাপ্লিকেশনটিকে Firebase এর সাথে সংযুক্ত করতে হবে।

Firebase Console এ প্রজেক্ট তৈরি:

  1. Firebase Console এ যান এবং লগ ইন করুন।
  2. একটি নতুন প্রজেক্ট তৈরি করুন।
  3. প্রজেক্ট তৈরি হলে, Firebase Cloud Messaging সক্রিয় করুন।

২. Firebase SDK ইনস্টল করা

Firebase SDK আপনার অ্যাপ্লিকেশনে যুক্ত করতে হবে। আমি এখানে Android এর জন্য উদাহরণ দেখাচ্ছি, তবে iOS এবং ওয়েবের জন্যও পদ্ধতি প্রায় একই।

Android অ্যাপে Firebase সেটআপ:

  1. Firebase SDK ইনস্টল করার জন্য প্রথমে আপনার build.gradle ফাইলে নিচের ডিপেনডেন্সি যুক্ত করুন:
// Project-level build.gradle
buildscript {
    repositories {
        google()  // Add this for Firebase SDK
    }
    dependencies {
        classpath 'com.google.gms:google-services:4.3.10' // Add this line
    }
}

// App-level build.gradle
apply plugin: 'com.google.gms.google-services'  // Add this at the bottom

dependencies {
    implementation 'com.google.firebase:firebase-messaging:23.1.1'  // Add Firebase Messaging
}
  1. Firebase Console থেকে google-services.json ফাইলটি ডাউনলোড করে আপনার অ্যাপের app/ ফোল্ডারে রাখুন।
  2. AndroidManifest.xml ফাইলে প্রয়োজনীয় পারমিশন এবং সার্ভিস যুক্ত করুন:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pushnotification">
    <application
        android:label="Push Notification Example"
        android:icon="@mipmap/ic_launcher">
        
        <service
            android:name=".MyFirebaseMessagingService"
            android:permission="android.permission.BIND_JOB_SERVICE">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        
    </application>
</manifest>

৩. FirebaseMessagingService তৈরি করা

FirebaseMessagingService একটি কাস্টম ক্লাস যা Firebase থেকে প্রাপ্ত নোটিফিকেশন হ্যান্ডল করার জন্য ব্যবহৃত হয়।

// MyFirebaseMessagingService.java

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // চেক করুন যদি মেসেজে ডেটা থাকে
        if (remoteMessage.getData().size() > 0) {
            // ডেটা প্রসেস করুন
            String message = remoteMessage.getData().get("message");
            // এখানে আপনার কোড যোগ করুন যেটি আপনাকে অ্যাপের UI-তে দেখাতে হবে
        }

        // যদি মেসেজে নোটিফিকেশন থাকে
        if (remoteMessage.getNotification() != null) {
            String title = remoteMessage.getNotification().getTitle();
            String body = remoteMessage.getNotification().getBody();
            // নোটিফিকেশন দেখানোর জন্য কোড যোগ করুন
        }
    }

    @Override
    public void onNewToken(String token) {
        // নতুন টোকেন আসলে Firebase এর সাথে সেটি আপডেট করুন
        // আপনি এটি সার্ভারে পাঠাতে পারেন
    }
}

৪. Push Notification পাঠানো

Firebase Console থেকে আপনি সরাসরি Push Notification পাঠাতে পারেন অথবা Firebase Cloud Messaging API ব্যবহার করে প্রোগ্রাম্যাটিকভাবে পাঠাতে পারেন।

Firebase Console থেকে Push Notification পাঠানো:

  1. Firebase Console এ গিয়ে Cloud Messaging ট্যাবে যান।
  2. Send your first message-এ ক্লিক করুন।
  3. আপনার Notification Title, Message লিখুন এবং টার্গেট ডিভাইস নির্বাচন করুন (যে ডিভাইসে আপনি নোটিফিকেশন পাঠাতে চান)।
  4. Send ক্লিক করুন।

Programmatically Push Notification পাঠানো:

আপনি Firebase Admin SDK ব্যবহার করে সার্ভার সাইডে Push Notification পাঠাতে পারেন। নিচে একটি উদাহরণ দেয়া হলো:

const admin = require("firebase-admin");

admin.initializeApp();

const message = {
  notification: {
    title: "New Notification",
    body: "This is a test message",
  },
  token: "DEVICE_REGISTRATION_TOKEN", // এই টোকেনটি ডিভাইস থেকে পাওয়া যাবে
};

admin.messaging().send(message)
  .then((response) => {
    console.log("Successfully sent message:", response);
  })
  .catch((error) => {
    console.log("Error sending message:", error);
  });

৫. Notification দেখানো

এখন, আপনার অ্যাপে MyFirebaseMessagingService ক্লাসে একটি notification তৈরি করুন এবং NotificationManager এর মাধ্যমে এটি দেখান।

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

public void showNotification(String title, String message) {
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String channelId = "default_channel";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Default", NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }
    
    Notification notification = new Notification.Builder(this, channelId)
            .setContentTitle(title)
            .setContentText(message)
            .setSmallIcon(R.drawable.ic_notification)
            .build();

    notificationManager.notify(0, notification);
}

সারাংশ

এখানে Firebase Cloud Messaging (FCM) ব্যবহার করে Push Notification পাঠানোর প্রক্রিয়া দেখানো হয়েছে। Firebase Console বা API ব্যবহার করে আপনি ডিভাইসের মধ্যে রিয়েল-টাইম নোটিফিকেশন পাঠাতে পারবেন। FirebaseMessagingService এর মাধ্যমে নোটিফিকেশন রিসিভ করা এবং NotificationManager ব্যবহার করে অ্যাপের UI তে দেখানো সম্ভব। Firebase-এ পুশ নোটিফিকেশন পাঠানোর প্রক্রিয়া সহজ, এবং এটি দ্রুত অ্যাপ্লিকেশন ব্যবহারকারীদের সাথে যোগাযোগের একটি শক্তিশালী মাধ্যম।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...